<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item><a href="/">用户管理</a></el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
      <el-row :gutter="10">
        <el-col :span="10">
          <el-input placeholder="请输入搜索内容" clearable suffix-icon="el-icon-search" v-model="queryInfo.query"
            @keypress.native.enter="getUserList" @clear="getUserList"></el-input>
        </el-col>
        <el-col :span="6">
          <el-button type="primary" @click="addFormVisible = true">添加用户</el-button>
        </el-col>
      </el-row>
      <el-table :data="tableData.userList" style="width: 100%">
        <el-table-column prop="id" label="ID" width="180">
        </el-table-column>
        <el-table-column prop="username" label="姓名" width="180">
        </el-table-column>
        <el-table-column prop="address" label="地址">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" circle size="mini" @click="showEditDialog(scope.row.id)">
            </el-button>
            <el-button type="danger" icon="el-icon-delete" circle size="mini" @click="deleteUserById(scope.row.id)">
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
          :current-page="queryInfo.currentpage" :page-sizes="[2, 4]" :page-size="queryInfo.pagesize"
          layout="total, sizes, prev, pager, next" :total="tableData.total">
        </el-pagination>
      </div>
    </el-card>
    <!--添加用户-->
    <el-dialog title="添加用户" :visible.sync="addFormVisible">
      <el-form ref="addFormRef" :model="userForm" label-width="80px" :rules="userFormRules">
        <el-form-item label="ID">
          <el-input v-model="userForm.id"></el-input>
        </el-form-item>
        <el-form-item label="姓名" prop="username">
          <el-input v-model="userForm.username"></el-input>
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="userForm.address"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addUser">确 定</el-button>
      </div>
    </el-dialog>

    <!--修改用户-->
    <el-dialog title="修改用户" :visible.sync="editFormVisible">
      <el-form ref="editFormRef" :model="userForm" label-width="80px" :rules="userFormRules">
        <el-form-item label="ID">
          <el-input v-model="userForm.id" disabled></el-input>
        </el-form-item>
        <el-form-item label="姓名" prop="username">
          <el-input v-model="userForm.username"></el-input>
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="userForm.address"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="editUser">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        queryInfo: {
          query: '',
          currentpage: 1,
          pagesize: 2
        },
        tableData: {
          userList: [],
          total: 0
        },
        //添加用户
        addFormVisible: false,
        editFormVisible: false,
        userForm: {
          id: 0,
          username: '',
          address: ''
        },
        userFormRules: {
          username: [{
              required: true,
              message: '请输入用户名',
              trigger: 'blur'
            },
            {
              min: 2,
              max: 10,
              message: '长度在 2 到 10 个字符',
              trigger: 'blur'
            }
          ]
        }
      }
    },
    created() {
      this.getUserList()
    },
    methods: {
      async getUserList() {
        const {
          data: res
        } = await this.$axios.post('/getUserList', this.queryInfo)
        console.log(res)
        this.tableData.userList = res.result
        this.tableData.total = res.total
      },
      handleSizeChange(size) {
        // console.log(size)
        this.queryInfo.pagesize = size
        this.getUserList()
      },
      handleCurrentChange(page) {
        // console.log(page)
        this.queryInfo.currentpage = page
        this.getUserList()
      },
      //添加用户
      addUser() {
        this.$refs.addFormRef.validate(async valid => {
          if (!valid) return
          const {
            data: res
          } = await this.$axios.post('/addUser', this.userForm)
          this.addFormVisible = false
          this.getUserList()
          this.$message.success("添加用户成功!!!")
        })
      },
      async showEditDialog(id) {
        // console.log(id)
        const {
          data: res
        } = await this.$axios.get('/getUserById', {
          data: {
            id: id
          }
        })
        this.userForm = res
        this.editFormVisible = true
      },
      editUser() {
        this.$refs.editFormRef.validate(async valid => {
          if (!valid) return
          const {
            data: res
          } = await this.$axios.post('/editUserById', this.userForm)
          this.editFormVisible = false
          this.getUserList()
          this.$message.success("修改用户成功!!!")
        })
      },
      deleteUserById(id) {
        console.log(id)
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          const {
            data: res
          } = await this.$axios.post('/deleteUserById', {
            id: id
          })
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getUserList()
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      }
    }
  }
</script>

<style>
  .box-card {
    margin-top: 15px;
  }

  .block {
    margin-top: 5px;
  }
</style>
